<?php

/*
 * This file will query those positions depending on the departmentId being passed.
 * 
 * Created by: Peter Agno Jr.
 * Date created: October 17, 2011
 * 
 * In parameters : departmentId
 * Out parameters : Array of positionId and positionName
 */

// Start - Checker for those users who will just go to the page by typing directly in the url.
if ($_POST) {
    // INCLUDES CONFIGURATION VARIABLES FOR DATABASE ACCESS
    include('../includes/siteConfig.php');
    
    // CONNECT TO DATABASE
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');
    
    // GET VARIABLES VIA POST METHOD
    $departmentId = $_POST['departmentId'];
    
    if ($departmentId != "All" ) {       
        // QUERY - Get positioning depending on the passed departmentId
        $query = 
            "
                SELECT DP.positionId as positionId, P.positionName as positionName
                FROM Department_Position as DP, Position as P
                WHERE DP.positionId = P.positionId AND DP.departmentId = '$departmentId'
                ORDER BY positionName
            ";
    }
    else {
        // QUERY - All the positions
        $query = 
            "
                SELECT positionId, positionName
                FROM Position
                ORDER BY positionName
            ";
    }
    
    
    
    $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    
    //THIS IDENTIFIES IF THERE EXIST POSITIONS.
    if ( mysql_num_rows($result) > 0 ) {
        // SAVE QUERY RESULTS TO AN ARRAY > $row   
        while ($row = mysql_fetch_array($result)) {
            $jsondata = array();
            $jsondata['positionId'] = $row['positionId'];
            $jsondata['positionName'] = $row['positionName'];

            $feed[] = $jsondata;
        }

        echo json_encode($feed);
    }
    else {
        echo json_encode(0);
    }
    
    mysql_free_result($result);
    mysql_close($connect);
}   // End - Checker for those users who will just go to the page by typing directly in the url.
else {
 echo "You are not authorized to view this page. This incident will be reported immediately.";
}
?>
